﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MainApp
{
    public partial class Product : DevExpress.XtraEditors.XtraUserControl
    {
        private Guid productid;
        private decimal minValue, maxValue;
        private string productName;

        private List<SpinEdit> newValues = new List<SpinEdit>();
        private List<DevExpress.XtraLayout.LayoutControlItem> newLayOut = new List<DevExpress.XtraLayout.LayoutControlItem>();

        public Product(Guid productid, string productName, decimal minValue, decimal maxValue)
        {
            InitializeComponent();
            this.productid = productid;
            this.maxValue = maxValue;
            this.minValue = minValue;
            spOldValue.EditValue = minValue;
            this.productName = productName;
            groupControl.Text = productName;
        }

        public void addSpinEdit(decimal value = 0)
        {
            DevExpress.XtraEditors.SpinEdit newEdit = new DevExpress.XtraEditors.SpinEdit();
            newEdit.EditValue = new decimal(new int[] { 0, 0, 0, 0 });
            newValues.Add(newEdit);
            //newEdit.Location = new System.Drawing.Point(343, 287);
            newEdit.Name = "spnewValue" + newValues.Count;
            newEdit.Value = value;
            spNewValue.Value += value;
            newEdit.ValueChanged += spNewVale_EditValueChanged;
            newEdit.Properties.Assign(spSample.Properties);

            layout.Controls.Add(newEdit);

            DevExpress.XtraLayout.LayoutControlItem newLay = layout.AddItem(newEdit.Name, newEdit);
            newLay.TextVisible = false;

            if (newLayOut.Count > 0)
            {
                newLay.Move(newLayOut[newLayOut.Count - 1], DevExpress.XtraLayout.Utils.InsertType.Bottom);
            }
            else
            {
                newLay.Move(layoutSample, DevExpress.XtraLayout.Utils.InsertType.Bottom);
            }
            newLayOut.Add(newLay);
            //newEdit.Focus();
        }

        private void spNewVale_EditValueChanged(object sender, EventArgs e)
        {
            decimal sum = 0;
            foreach (SpinEdit item in newValues)
            {
                if (item.EditValue != null)
                {
                    sum += item.Value;
                }
            }
            spNewValue.Value = sum;
            if (sender == newValues[newValues.Count - 1])
            {
                addSpinEdit();
            }
            if (ValueChanged != null)
            {
                ValueChanged.Invoke(sender, null);
            }
            
            lblWarning.Visible = !isValid;
        }

        private bool checkValid()
        {
            if (spNewValue.Value < minValue)
            {
                lblWarning.Text = "Chỉ số mới không thể nhỏ hơn chỉ số cũ";
                return false;
            }
            if (spNewValue.Value > maxValue)
            {
                lblWarning.Text = "Vượt quá số xăng trong kho";
                return false;
            }
            return true;
        }
        public bool isValid { get { return checkValid(); } }
        public event EventHandler ValueChanged;
        public Guid ProductID { get { return productid; } }
        public decimal TotalInput { get { return spNewValue.Value - spOldValue.Value; } }
    }
}
